﻿using System;
using System.Drawing;
using JamesZhao.Engine.GameObjects;
using JamesZhao.Engine.Graphics;
using JamesZhao.Engine.Graphics.Surfaces;
using SlimDX.Direct2D;
using SlimMath;

namespace JamesZhao.Engine.GameComponents
{
    public class DisplayFpsComponent : GameComponent, IOverlayDrawable
    {
        private SolidColorBrush _colorBrush;
        private PathGeometry _geomPath;

        public DisplayFpsComponent(GameObject owner) : base(owner)
        {
        }

        public override void Initialize()
        {
            Engine.GraphicsManager.VisualsList.AttachOverlay(this);
        }

        public override void Update()
        {
        }


        public void DrawOverlay(ColorSurface surface)
        {
            surface.Target2D.BeginDraw();
            surface.Target2D.FillGeometry(_geomPath, _colorBrush);
            surface.Target2D.EndDraw();
        }

        public bool IsInitialized { get; set; }

        public void Initialize(ColorSurface surface)
        {
            _colorBrush = surface.CreateSolidColorBrush(new Color4(1));
            _geomPath = surface.CreatePathGeometry();

            using (var sink = _geomPath.Open())
            {
                var p0 = new PointF(100, 100);
                var p1 = new PointF(100, 200);
                var p2 = new PointF(200, 200);

                sink.BeginFigure(p0, FigureBegin.Filled);
                sink.AddLine(p1);
                sink.AddLine(p2);
                sink.EndFigure(FigureEnd.Closed);

                // Note that Close() and Dispose() are not equivalent like they are for
                // some other IDisposable() objects.
                sink.Close();
            }
            IsInitialized = true;
        }

    }
}
